﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WEBWeixinSDK;
using WEBWeixinSDK.Plug;

namespace WxLogin
{
    public partial class TabMainForm : Form
    {
        public TabMainForm()
        {
            InitializeComponent();
        }
         public    int namenum = 1;
        private void TabMainForm_Load(object sender, EventArgs e)
        {
            //ssToolStripMenuItem.Visible = false;
            WXsql.GetMsg("wxuin");
            WXsql.GetMsg("UserAgent");
            WXsql.GetMsg("autoMsg");
            if (IsHandleCreated)
            {
                Weixin.BELoginStatus(this, LoginStatus);
            }
            this.notifyIcon1.Visible = false;
            notifyIcon1.Visible = true ;
        }
        public void LoginStatus(Weixin.LoginStatus status)
        {
            TabMainForm f2 = new TabMainForm();
            switch (status)
            {
                case Weixin.LoginStatus.LoginSuccess:

                    SsToolStripMenuItem_Click( );
                    break;

            }
        }
      
   
        private void 登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            namenum =+ tabControl1.TabCount;
            FormDL2 f2 = new FormDL2();
            f2.Show();
        
          
        }

        public void SsToolStripMenuItem_Click()
        {
            //Add("账号",new MainForm());
            MainL objfrm = new MainL();
            AddTabControl("MainLtab",  objfrm); 
        }

        /// </summary>
        /// <param name="MainTabControlKey">选项卡的键值</param>
        /// <param name="objTabControl">要添加到的TabControl对象</param>
        /// <returns></returns>
        private Boolean ErgodicModiForm(string MainTabControlKey, TabControl objTabControl)
        {
            //遍历选项卡判断是否存在该子窗体
            foreach (Control con in objTabControl.Controls)
            {
                TabPage tab = (TabPage)con;
                if (tab.Name == MainTabControlKey)
                {
                    return false;//存在
                }
            }
            return true;//不存在
        }
        /// <summary>
        /// 添加一个选项卡
        /// </summary>
        /// <param name="MainTabControlKey">选项卡的键值</param>
        /// <param name="MainTabControlName">选项卡要显示的名称</param>
        /// <param name="objTabControl">要添加到的TabControl对象</param>
        /// <param name="objfrm">要被添加到选项卡的用户控件对象</param>
        public void AddTabControl(string MainTabControlKey, UserControl objfrm)
        {

            try
            {
               
                string tabnum = tabControl1.TabPages.Count.ToString();
                //if (ErgodicModiForm(MainTabControlKey, tabControl1))
                //{
                    //声明一个选项卡对象
                    TabPage tabPage = new TabPage
                    {
                        //选项卡的名称
                        Name = Weixin.Uin ,
                        //选项卡的文本
                        Text = Weixin.NickNamestr
                    };
                    //向选项卡集合添加新选项卡
                    tabControl1.Controls.Add(tabPage);
                    //子窗体显示
                    objfrm.Show();
                    //子窗体大小设置为选项卡大小
                    objfrm.Size = tabPage.Size;
                    //将子窗体添加到选项卡中
                    tabPage.Controls.Add(objfrm);
                    tabControl1.Dock = DockStyle.Fill;
                    objfrm.Dock = DockStyle.Fill;
                    //设置当前选项卡为新增选项卡
                    tabControl1.SelectedTab.Name = MainTabControlKey;
            //  notifyIcon1.Visible = false;
            //  this.  notifyIcon1.Visible = true;
            }
            catch (Exception)
            {
             }
        }

        private void 测试ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MainL objfrm = new MainL();
            AddTabControl("MainLtab", objfrm);

        }


        #region  窗口事件处理

        #region 私有方法　处理窗体的　显示　隐藏　关闭(退出)
        private void ExitMainForm()
        {
            if (MessageBox.Show("是否退出微信多开？", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        private void HideMainForm()
        {
            this.Hide();
        }

        private void ShowMainForm()
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }
        #endregion

        #region 右键菜单处理，显示　隐藏　退出
        private void Menu_Notify_Click(object sender, EventArgs e)
        {
            ShowMainForm();
        }
        private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HideMainForm();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;
            ExitMainForm();
        }
        #endregion

        #region 双击托盘上图标时，显示窗体
        private void NotifyIcon1_DoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;

                HideMainForm();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                ShowMainForm();
            }
        }
        #endregion

        #region 点最小化按钮时，最小化到托盘
        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideMainForm();
            }
        }
        #endregion

        #region 窗体关闭时最小化到托盘
        private void MainForm_FormClosing_1(object sender, FormClosingEventArgs e)
        {

            e.Cancel = true;

            HideMainForm();
        }


        #endregion

        #endregion

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowMainForm();
        }
    }
}
